﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.NetworkInformation;

namespace iMan
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MIB_TCPSTATS
    {
        public int dwRtoAlgorithm;
        public int dwRtoMin;
        public int dwRtoMax;
        public int dwMaxConn;
        public int dwActiveOpens;
        public int dwPassiveOpens;
        public int dwAttemptFails;
        public int dwEstabResets;
        public int dwCurrEstab;
        public int dwInSegs;
        public int dwOutSegs;
        public int dwRetransSegs;
        public int dwInErrs;
        public int dwOutRsts;
        public int dwNumConns;
    }

    public struct MIB_TCPTABLE
    {
        public int dwNumEntries;
        public MIB_TCPROW[] table;
    }

    public struct MIB_TCPROW
    {
        public string StrgState;
        public int iState;
        public IPEndPoint Local;
        public IPEndPoint Remote;
    }

    public struct MIB_EXTCPTABLE
    {
        public int dwNumEntries;
        public MIB_EXTCPROW[] table;
    }

    public struct MIB_EXTCPROW
    {
        public string StrgState;
        public int iState;
        public IPEndPoint Local;
        public IPEndPoint Remote;
        public int dwProcessId;
        public string ProcessName;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MIB_TCPTABLE_OWNER_MODULE
    {
        public uint dwNumEntries;
        public MIB_TCPROW_OWNER_MODULE[] table;
    }

    public struct MIB_TCPROW_OWNER_MODULE
    {
        public const int TCPIP_OWNING_MODULE_SIZE = 16;
        public uint dwState;
        public IPEndPoint Local;
        public IPEndPoint Remote;
        public uint dwOwningPid;
        public uint liCreateTimestamp;
        public ulong[] OwningModuleInfo;
    }

    public struct MIB_TCPTABLE_OWNER_PID
    {
        public int dwNumEntries;
        public MIB_TCPROW_OWNER_PID[] table;
    }

    public struct MIB_TCPROW_OWNER_PID
    {
        public int dwState;
        public IPEndPoint Local;
        public IPEndPoint Remote;
        public int dwOwningPid;
        public string State;
        public string ProcessName;
    }

    public enum TCP_TABLE_CLASS
    {
        TCP_TABLE_BASIC_LISTENER,
        TCP_TABLE_BASIC_CONNECTIONS,
        TCP_TABLE_BASIC_ALL,
        TCP_TABLE_OWNER_PID_LISTENER,
        TCP_TABLE_OWNER_PID_CONNECTIONS,
        TCP_TABLE_OWNER_PID_ALL,
        TCP_TABLE_OWNER_MODULE_LISTENER,
        TCP_TABLE_OWNER_MODULE_CONNECTIONS,
        TCP_TABLE_OWNER_MODULE_ALL,
    }
}
